/**
 * 
 */
package com.yidu.drug.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Message;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugMaterialsModelMapper;
import com.yidu.drug.dao.DrugModelMapper;
import com.yidu.drug.dao.DrugProductionModelMapper;
import com.yidu.drug.dao.ProductionModelMapper;
import com.yidu.drug.dao.RepertoryModelMapper;
import com.yidu.drug.model.DrugMaterialsModel;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.DrugProductionModel;
import com.yidu.drug.model.ProductionModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.service.DrugProductionService;

/**
 * 生产药品计划Service实现类
 * @author 王志伟
 * @date 2018年8月30日 上午9:28:35
 * @version 1.0
 */
@Service
public class DrugProductionServiceImpl implements DrugProductionService{
	
	//药品生产计划的DAO
	@Resource
	private DrugProductionModelMapper drpDao;

	//生产计划DAO
	@Resource
	private ProductionModelMapper proDao;
	
	//药品DAO
	@Resource
	private DrugModelMapper drugDao;
	
	//药品材料DAO
	@Resource
	private DrugMaterialsModelMapper dmDao;
	
	//库存DAO
	@Resource
	private RepertoryModelMapper repDao;
	
	//消息类
	Message m=new Message();
	
	@Override
	public int deleteByPrimaryKey(String drpId) {
		// TODO Auto-generated method stub
		return drpDao.deleteByPrimaryKey(drpId);
	}

	@Override
	public int insert(DrugProductionModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(DrugProductionModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public DrugProductionModel selectByPrimaryKey(String drpId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(DrugProductionModel record) {
		return drpDao.updateByPrimaryKeySelective(record);
	}


	@Override
	public int updateByPrimaryKey(DrugProductionModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> findByProId(String limit, String page, String proId) {
		DrugProductionModel drp=new DrugProductionModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		drp.setPage(pa);
		drp.setProId(proId);
		List<DrugProductionModel> list=drpDao.findByProId(drp);
		for (DrugProductionModel drugProductionModel : list) {
			String drugId=drugProductionModel.getDrugId();
			
			DrugModel drug=drugDao.selectByPrimaryKey(drugId);
			drugProductionModel.setDrugName(drug.getDrugName());
			
			ProductionModel pro=proDao.selectByPrimaryKey(proId);
			drugProductionModel.setEntName(pro.getEntName());
		}
		//查询出的信息的总行数
		Long count = drpDao.findRows(drp);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}

	@Override
	public int updateOrAdd(String a) {
		Gson gosn=new Gson();
		//使用Type类，取得相应类型对象的class属性
		java.lang.reflect.Type listType=new TypeToken<ArrayList<DrugProductionModel>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
		//使用集合接受将json转换为java对象集合后的数据
		ArrayList<DrugProductionModel> list = gosn.fromJson(a, listType);
		
		int rows=0;
		for (DrugProductionModel drugProductionModel : list) {
			System.out.print("主键ID"+drugProductionModel.getDrpId());
			System.out.print(" 药品ID"+drugProductionModel.getDrugId());
			System.out.print(" 生产ID"+drugProductionModel.getProId());
			System.out.println(" 药品数量"+drugProductionModel.getDrpNumber());
			System.out.println("==");
			
			List<DrugMaterialsModel> matList=dmDao.findMatIdByDrugId(drugProductionModel.getDrugId());
			for (DrugMaterialsModel drugMaterialsModel : matList) {
				RepertoryModel rep=repDao.findByPwId(drugMaterialsModel.getMatId());
				System.out.println("药品所需材料数量+"+drugMaterialsModel.getDmNumber());
				System.out.println("药品数量+"+drugProductionModel.getDrpNumber());
				System.out.println("库存数量+"+rep.getRepNumber());
				if(drugMaterialsModel.getDmNumber()*drugProductionModel.getDrpNumber()>rep.getRepNumber()){
					rows=-1;
				}
			}
			if(rows<0){
				DrugProductionModel drp=new DrugProductionModel();
				drp.setDrpId(drugProductionModel.getDrpId());
				drp.setDrpNumber(0L);
				drpDao.updateByPrimaryKeySelective(drp);
			}
			if(rows>=0){
				DrugProductionModel drp=drpDao.selectByPrimaryKey(drugProductionModel.getDrpId());
				if(drp==null){
					drugProductionModel.setDrpId(Tools.getRandomUUID());
					drugProductionModel.setIsva(Tools.ISVA_YES);
					rows=drpDao.insertSelective(drugProductionModel);
				}else{
					rows=drpDao.updateByPrimaryKeySelective(drugProductionModel);
				}
			}
		}
		return rows;
	}

	
	@Override
	public List<DrugProductionModel> findDrugIdByProId(String proId) {
		List<DrugProductionModel> list=drpDao.findDrugIdByProId(proId);
		for (DrugProductionModel drugProductionModel : list) {
			String drugId=drugProductionModel.getDrugId();
			DrugModel drug=drugDao.selectByPrimaryKey(drugId);
			drugProductionModel.setDrugName(drug.getDrugName());
		}
		return list;
	}

	@Override
	public int addOrDelete(String drugIds,String proId) {
		List<DrugProductionModel> list=drpDao.findDrugIdByProId(proId);
		String drugId[] = drugIds.split(",");
		System.out.println("药品ID的长度"+drugId.length);
		List<DrugProductionModel> listOne = new ArrayList<>();
		for(int i=0; i<drugId.length; i++){
			System.out.println("页面药品ID+"+drugId[i]); 
			boolean flag = false;
			
			for(int j=0; j<list.size(); j++){
				System.out.println("数据库药品ID+"+list.get(j).getDrugId());
				if(drugId[i].equals(list.get(j).getDrugId())){
					//如果相等 加入数据库的信息
					listOne.add(list.get(j));
					flag = true;
				}
			}
			if(!flag){
				//如果为false 不相等 就加入listOne集合中
				DrugProductionModel drp=new DrugProductionModel();
				drp.setDrpId(Tools.getRandomUUID());
				drp.setProId(proId);
				drp.setDrugId(drugId[i]);
				drp.setDrpNumber(0L);
				listOne.add(drp);
			}
		}
		//先根据药品ID删除所有的药品材料配置
		drpDao.deleteByProId(proId);
		int counts=0;
		for(int i=0; i<listOne.size(); i++){
			//在listOne循环加入所有的数据
			counts =drpDao.insertSelective(listOne.get(i));
		}
		return counts;
	}
}
